package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.common.PageInfo;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.pojo.Employee;
import com.itheima.service.EmploeeService;
import com.itheima.vo.EmployeeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;





@Slf4j
@Service
public class EmploeeServiceImpl implements EmploeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public EmployeeVo login(String username, String password) {

        Employee employee = employeeMapper.getEmployee(username, password);
        if (employee==null){
            return null;
        }
        //生成token
        String token = "employee_token"+System.currentTimeMillis() + employee.getId();
        //存入数据库
        // Map<String,Object> map=new HashMap<>();
        // map.put("username", "admin");
        // map.get("username");
        //redisTemplate的方法，跟MAp中put get 一样 //opsForValue()  存的是String
        redisTemplate.opsForValue().set("employee_token:"+token, JSON.toJSONString(employee) );

        employeeMapper.updateToken(token, employee.getId());
        //封装数据
        EmployeeVo employeeVO = new EmployeeVo();
        BeanUtils.copyProperties(employee, employeeVO);
        employeeVO.setToken(token);
        return employeeVO;
    }

    @Override
    public void clearToken(String token) {
        Employee emp = employeeMapper.getByToken(token);
        employeeMapper.clearToken(emp.getId());
    }

    @Override
    public EmployeeVo getById(String id) {
        Employee employee = employeeMapper.getById(id);
        if (employee==null){
            return null;
        }
        EmployeeVo employeeVO=new EmployeeVo();
        BeanUtils.copyProperties(employee,employeeVO);
        return employeeVO;
    }

    @Override
    public PageInfo<Employee> page(String name, Integer page, Integer pageSize) {
        PageInfo<Employee> pageInfo=new PageInfo<>();
        Integer start =(page-1) * pageSize;
        List<Employee> employees = employeeMapper.page(name, start, pageSize);
        pageInfo.setRecords(employees);
        pageInfo.setSize(pageSize);
        pageInfo.setCurrent(page);
        long count = employeeMapper.countEmployee(name);
        log.info("你查询的{}",count);
        pageInfo.setTotal(count);
        return pageInfo;
    }
        //添加
    @Override
    public boolean save(Employee employee) {
        String id = IdUtil.getSnowflakeNextIdStr();
        if (!(employee.getId()==null)){
            return false;
        }
        employee.setId(id);
        int addemployee = employeeMapper.save(employee);
        return addemployee>0;
    }

    @Override
    public boolean update(Employee employee) {
        if (employee.getId()==null){
            return false;
        }
        LocalDateTime ldt = LocalDateTime.now();
        employee.setUpdateTime(ldt);
        Integer upd = employeeMapper.update(employee);
        return upd>0;
    }
    @Override
    public Employee getAllById(String id){
        Employee byId = employeeMapper.getById(id);
        if (byId==null){
            return null;
        }
        return byId;
    }

}
